(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.limit.limitTree.updateController', updateController);
	updateController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'limitService' ];
	function updateController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, limitService) {
		var ctrl = this, limitInfo = $scope.$stateParams.params, errorMessage = {};
//		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.limit = {};
		ctrl.init = function() {
			var params = {
					limitId : limitInfo.limitId
			};
			authService.update(params).then(function(response) {
				var initLimit = response.data || limitInfo;
				// init general
				limitService.initGeneral(initLimit, $scope, ctrl).then(function() {
					// change layout for dynaparm by limitNode
					ctrl.changeLayoutConfig(initLimit, "update").then(function() {
						// init limitRule
						limitService.initLimitRule($scope, ctrl);
						ctrl.limit = initLimit;
					});
				});
			});
		};
		

		ctrl.updateSave = function() {
			if (ctrl.limit.taskId) {
				return authService.updateContinueSave(ctrl.limit).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.limit).then(saveSuccessful, saveFailed);
			} 
		};

		ctrl.updateSubmit = function() {
			if (ctrl.limit.taskId) {
				return authService.updateContinueSubmit(ctrl.limit).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.limit).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.limit.limitTree');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.limit.list',
					'state' : 'cda.limit.limitTree',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.limitHis = function(){
			$scope.go('cda.limit.limitTree.hisList', ctrl.limit);
		};
		ctrl.relatedTxn = function(){
			$scope.go('cda.transaction.list', {'limit' : ctrl.limit});
		};
		ctrl.subLimit = function(){
			var limitInfo = $scope.$parent.vm.limitTreeApi.get_selected_branch()[0];
			if (limitInfo.children && limitInfo.children.length > 0) {
				if (limitInfo.children.length > 1) {
					// params
					var __params = {'childLimitNodes' : limitInfo.children};
					modalService.openCustomDialogs({
						'title': 'Child LimitNode Edit',
						'dialogsModule' : 'cda.limit.childNodeEdit',
						'params': __params,
						'btnCallback' : {
							'ok' : function(limitNode) {							
								if (limitNode && limitNode.limitNodeId) {
									for (var i = 0; i < limitInfo.children.length; i++) {
										if (limitInfo.children[i].id == limitNode.limitNodeId) {
											var nextNodeInfo = limitInfo.children[i];
											nextNodeInfo.parentLimitId = ctrl.limit.limitId;
											//select next branch
											var trueBranch = selectTrueBranch($scope.$parent.vm.limitData, nextNodeInfo);
											$scope.$parent.vm.limitTreeApi.select_branch(trueBranch);
										}
									}
								}
							}
						}		
					});
				} else {
					var nextNodeInfo = limitInfo.children[0];
					nextNodeInfo.parentLimitId = ctrl.limit.limitId;
					//select next branch
					var trueBranch = selectTrueBranch($scope.$parent.vm.limitData, nextNodeInfo);
					$scope.$parent.vm.limitTreeApi.select_branch(trueBranch);
				}
			} else {
				modalService.openErrorDialogs({
					messages : [ 'It has no sub-limit. To create sub-limit, please create a node first.' ]
				});
				return;
			}
		};
		
		function selectTrueBranch(limitData, nextNodeInfo) {
			for(var i =0 ; i < limitData.length ; i++){
				if(limitData[i].uid == nextNodeInfo.uid){
					return limitData[i];
				}else if(limitData[i].children && limitData[i].children.length > 0){
					var date = selectTrueBranch(limitData[i].children, nextNodeInfo);
					if(date != null){
						return date;
					};
				};
			};
		}
		
	}
})(angular);
